package com.learning.thread.juc.executor.defined;

/**
 * ClassName: CustomThreadPoolExecutor
 * Description: 自定义任务线程，有优先级，实现 Comparable 接口
 * Date: 2016/11/26 17:45
 *
 * @author SAM SHO
 * @version V1.0
 */
public class CustomTaskThread implements Runnable, Comparable<CustomTaskThread> {

    private String name;

    public CustomTaskThread() {
    }

    public CustomTaskThread(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(100);
            System.out.println(name + " ");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


    @Override
    public int compareTo(CustomTaskThread o) {
        //线程名称中标注的优先级
        int me = Integer.parseInt(this.name.split("_")[1]);
        int other = Integer.parseInt(o.name.split("_")[1]);

        if (me > other) {
            return 1;
        } else if (me < other) {
            return -1;
        } else {
            return 0;
        }
    }
}
